#include "adminwindow.h"
#include "ui_adminwindow.h"
#include "addstationdialog.h"
#include "addtraindialog.h"
#include "tr_addstationdialog.h"
#include "tk_addticketdialog.h"
#include "ui_addstationdialog.h"
#include "ui_addtraindialog.h"
#include "ui_tr_addstationdialog.h"
#include "ui_tk_addticketdialog.h"

#include "StationManagement.h"
#include "trainmanagement.h"
#include "ticketmanagement.h"
#include "DataStruct.h"

#include <iostream>

using namespace std;


AdminWindow::AdminWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::AdminWindow)
{
    ui->setupUi(this);
}

AdminWindow::~AdminWindow()
{
    delete ui;
}


void AdminWindow::on_st_query_button_clicked()
{
    QString queryConditon = ui->st_query_edit->text();

    //Determine whether null or empty
    if(queryConditon.isNull()||queryConditon.isEmpty())
        return;

    Station station = StationManagement::queryStation(queryConditon);

    ui->st_idContent_label->setText(QString::number(station.ID));
    ui->st_nameContent_edit->setText(station.name);

    return;
}

void AdminWindow::on_st_add_button_clicked()
{
    AddStationDialog addDialog(this);
    addDialog.setModal(true);
    addDialog.exec();
}

void AdminWindow::on_st_delete_button_clicked()
{
    QString id = ui->st_idContent_label->text();

    if(id.isNull()||id.isEmpty())
        return;

    StationManagement::deleteStation(id);

    ui->st_idContent_label->clear();
    ui->st_nameContent_edit->clear();
}

void AdminWindow::on_st_save_hutton_clicked()
{
    QString id = ui->st_idContent_label->text();

    if(id.isNull()||id.isEmpty())
        return;

    QString name = ui->st_nameContent_edit->text();

    StationManagement::modifyStation(id,name);
}

void AdminWindow::on_tr_query_button_clicked()
{
    QString trainNum = ui->tr_query_edit->text();

    if(trainNum.isNull() || trainNum.isEmpty())
        return;

    TrainManagement::queryTrain(trainNum);

    ui->tr_trainNumContent_label->setText(TrainManagement::getTrainNum());

    refreshStationList();
}

void AdminWindow::on_tr_delStation_button_clicked()
{
    int deleteIndex = ui->tr_station_table->currentRow();

    TrainManagement::deleteStation(deleteIndex);

    ui->tr_station_table->removeRow(deleteIndex);
}

void AdminWindow::on_tr_addStation_button_clicked()
{
    Tr_AddStationDialog tr_add(ui->tr_station_table->currentRow(),this);
    tr_add.setModal(true);
    tr_add.exec();

    refreshStationList();
}

void AdminWindow::on_tr_add_button_clicked()
{
    AddTrainDialog addtrainDialog(this);
    addtrainDialog.setModal(true);
    addtrainDialog.exec();

    ui->tr_trainNumContent_label->setText(TrainManagement::getTrainNum());
    TrainManagement::clearTrain();
    refreshStationList();

}

void AdminWindow::on_tr_del_button_clicked()
{
    TrainManagement::deleteTrain();
    ui->tr_trainNumContent_label->clear();
}

void AdminWindow::on_tr_save_button_clicked()
{
    TrainManagement::modifyTrain();
}

void AdminWindow::refreshStationList()
{
    QString* stationList = TrainManagement::getTrainList();

    ui->tr_station_table->clearContents();

    if(stationList == NULL)
        return;

    int length = stationList[0].toInt();

    ui->tr_station_table->setRowCount(length);


//    cout << stationList->length() << endl;


    for(int i = 0; i < length * 2; i ++)
    {
        ui->tr_station_table->setItem(i/2, i%2, new QTableWidgetItem(stationList[i + 1]));
    }

    delete[] stationList;
}

void AdminWindow::on_tk_delete_button_clicked()
{
    TicketManagement::deleteTicket();

    ui->tk_trainNumContent_label->clear();
    ui->tk_satrtSt_comBox->clear();
    ui->tk_arrSt_comBox->clear();
    ui->tk_price_edit->clear();
    ui->tk_seatNum_edit->clear();
}

void AdminWindow::on_tk_save_button_clicked()
{
    TicketManagement::modifyTicket(ui->tk_satrtSt_comBox->currentIndex(),
                                   ui->tk_arrSt_comBox->currentIndex(),
                                   ui->tk_seatNum_edit->text().toInt(),
                                   ui->tk_price_edit->text().toDouble());
}

void AdminWindow::on_tk_add_button_clicked()
{
    TK_AddTicketDialog addTiketDialog(this);

    connect(&addTiketDialog,SIGNAL(trainNum(QString)),this,SLOT(on_tk_receive_trainNum(QString)));
    addTiketDialog.setModal(true);
    addTiketDialog.exec();
}

void AdminWindow::on_tk_receive_trainNum(QString trainNum)
{
   QString* stationList = TicketManagement::addTicket(trainNum);

   ui->tk_arrSt_comBox->clear();
   ui->tk_satrtSt_comBox->clear();

   int length = stationList[0].toInt();

   for(int i = 0; i < length - 1 ; i++)
   {
       ui->tk_satrtSt_comBox->insertItem(i,stationList[i+1]);
       ui->tk_arrSt_comBox->insertItem(i,stationList[i+2]);
   }
   ui->tk_satrtSt_comBox->insertItem(length-1,stationList[length]);
}

void AdminWindow::on_tk_query_button_clicked()
{
    QString ticketID = ui->tk_query_edit->text();

    on_tk_receive_trainNum(TicketManagement::queryTicket(ticketID));
}
